import React,{ createContext,useReducer } from 'react'


export const MyContext = createContext();

const reducer = (state,action)=> {
    switch (action.type) {
        case 'setName':
            return {
                ...state, name:action.name
            }
        case 'setAge':
            return {
                ...state, age:action.age
            }
        default:
            return state;
    }
}

const defaultData = {
    name:'霖垢',
    age:14
}


export const Reducer = (props)=>{

    let [state,dispatch] = useReducer(reducer,defaultData)
    return(
         // ! 要注意 value这里是一个对象 { { 这样的 } }
        <MyContext.Provider value={{state,dispatch}}>
            {props.children}
        </MyContext.Provider>
    )
}